package ch.trackedbean.client;

import static ch.trackedbean.client.resources.Messages.*;
import java.awt.event.*;
import java.util.*;
import ch.trackedbean.binding.action.*;
import ch.trackedbean.client.gui.*;
import ch.trackedbean.client.views.*;
import ch.trackedbean.server.data.to.*;

/**
 * Something like a plugin.
 * 
 * @author hautle
 */
public class APlugin {
    /**
     * Initializes the 'plugin'.
     */
    public void init() {
        registerActions();
    }

    /**
     * Registers the actions.
     */
    private void registerActions() {
        MainFrame.ME.addAction(new ServerAction<Void, Void, List<PersonShort>>("Persons") {
            @Override
            protected String getProgressBarText() {
                return "Loading person list...";
            }

            @Override
            public List<PersonShort> perform(ActionEvent e, Void prepared, List<Void> selection) throws Exception {
                return ServerAccess.ME.getSrv().getPersons();
            }

            @Override
            protected void finishHook(ActionEvent e, Void prepare, List<PersonShort> result, List<Void> selection) {
                final PersonList l = new PersonList();
                l.setModel(result);
                MainFrame.ME.showWindow(PERSON_LIST_VIEW, l);
            }
        });
        ActionManager.registerAction(PersonShort.class, new ServerAction<PersonShort, Void, PersonTO>("Detail") {
            @Override
            protected String getProgressBarText() {
                return "Loading person detail...";
            }

            @Override
            public String getActionID() {
                return PersonDetail.DETAIL_ACTION_ID;
            }

            @Override
            protected boolean showActionForSelection(List<PersonShort> selection) {
                return selection.size() == 1;
            }

            @Override
            public PersonTO perform(ActionEvent e, Void prepared, List<PersonShort> selection) throws Exception {
                return ServerAccess.ME.getSrv().getPerson(selection.get(0).getKey());
            }

            @Override
            protected void finishHook(ActionEvent e, Void prepare, PersonTO result, List<PersonShort> selection) {
                final PersonDetail l = new PersonDetail();
                l.setModel(result);
                MainFrame.ME.showWindow(PERSON_DETAIL_VIEW, l);
            }
        });
        ActionManager.registerAction(PersonShort.class, new MenuPathBuilder().add("a").add("b").group("q").get(), 0, new AbstractSelectionAction("Foo") {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
            }
        });
        ActionManager.registerAction(PersonShort.class, new MenuPathBuilder().add("a").add("b").get(), 0, new AbstractSelectionAction("Bar") {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

            }
        });
    }
}
